<?php
namespace app\admin\controller;
use app\service\RoleService;
use think\exception\ValidateException;
use app\admin\validate\RoleValidate;
class Role extends Admin
{
    public function __construct(RoleService $service)
    {
        parent::__construct();
        $this->service = $service;
    }
    public function index()
    {
        if (!$this->request->isPost()) {
            $this->assign("title","角色列表");
            return $this->fetch("index");
        }
        $params = $this->request->post();
        $where =[];
        if (!empty($params["name"])) {
            $where[] = ['name', 'like', "%{$params['name']}%"];
        }
        $ret = $this->service->getList($where);
        $this->result($ret);
    }

    private function save()
    {
        $params = $this->request->Post();
        $data =[
            "name"=>$params["name"],
            "id"=>isset($params["id"]) ? $params["id"]:0,
            "nodes"=>$params["nodes"],
            "desc"=>$params["desc"]
        ];
        $action = $this->request->action();
        try {
            validate(RoleValidate::class)->scene($action)->check($data);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        unset($data["nodes"]);
        $data["privileges"] = implode(",",$params["nodes"]);
        $ret = $this->service->save($data);
        if ($ret) {
            $this->success("保存成功");
        } else {
            $this->error("保存失败");
        }
    }

    public function add()
    {
        if (!$this->request->isPost()) {
            $this->assign("title","添加角色");
            $this->assign("vo",null);
            return $this->fetch("form");
        }
        $this->save();
    }

    public function edit()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get("id");
            if (!$id) {
                throw new ValidateException("缺少id");
            }
            $vo = $this->service->get($id);
            $this->assign("vo",$vo);
            $this->assign("title","编辑角色");
            return $this->fetch("form");
        }
        $this->save();
    }

    public function privileges()
    {
        $id = $this->request->post("id");
        if (!$id) {
            throw new ValidateException("缺少id");
        }
        $menuService = $this->app->make(\app\service\MenuService::class);
        $menus = $menuService->getAll(["status"=>1],"*","id asc")->toArray();
        if (!empty($id)) {
            $vo = $this->service->get($id);
            $nodes = explode(",",$vo->privileges);
            foreach ($menus as &$v) {
                $v["checked"] = false;
                if (in_array($v["id"],$nodes)) {
                    $v["checked"] = true;
                }
            }
        }
        $menus = list_to_tree($menus);
        return json(["code"=>0,"data"=>$menus]);
    }

    public function status()
    {
        $params = $this->request->Post();
        try {
            validate(RoleValidate::class)->scene("status")->check($params);
        } catch (ValidateException $e) {
            $this->error($e);
        }
        $ret = $this->service->setStatus($params["id"],$params["status"]);
        if ($ret) {
            $this->success("设置成功");
        } else {
            $this->error("设置失败");
        }
    }

    public function remove()
    {
        $params = $this->request->Post();
        try {
            validate(RoleValidate::class)->scene("remove")->check($params);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        $ret = $this->service->remove($params["id"]);
        if ($ret) {
            $this->success("设置成功");
        } else {
            $this->error("设置失败");
        }
    }

}